package model

import (
	"fmt"
	"gin_vue_blog/utils/errmsg"
	"gorm.io/gorm"
)

type Article struct {
	Category Category `gorm:"foreignKey:Cid"`
	gorm.Model
	Title        string `gorm:"type:varchar(100);not null" json:"title" validate:"required,max:40"`
	Cid          uint   `json:"cid"`
	Desc         string `gorm:"type:varchar(200)" json:"desc"`
	Content      string `gorm:"type:longtext" json:"content" validate:"required"`
	Img          string `gorm:"type:varchar(100)" json:"img"`
	CommentCount int    `gorm:"type:int;not null;default:0" json:"comment_count"`
	PageView     int    `gorm:"type:int;not null;default:0" json:"read_count"`
}

// 添加文章

func CreateArt(data *Article) int {
	err := db.Create(&data)
	if err == nil {
		return errmsg.ERROR
	}
	return errmsg.SUCCESS
}

// 查询分类下的所有文章

func GetCateArtList(pageSize, pageNum int, cid int) ([]Article, int, int64) {
	var articleList []Article
	var total int64
	err := db.Preload("Category").Where("cid = ?", cid).Limit(pageSize).Offset(pageSize * (pageNum - 1)).Find(&articleList).Error
	fmt.Println("articleList:", articleList)
	db.Model(&Article{}).Where("cid = ?", cid).Count(&total)
	if err != nil {
		return nil, errmsg.ERROR_CATEGORY_NOT_EXIST, 0
	}
	return articleList, errmsg.SUCCESS, total
}

// 查询单个文章

func GetArt(id int) (Article, int) {
	var article Article
	err := db.Preload("Category").Where("id = ?", id).First(&article).Error
	db.Model(&Article{}).UpdateColumn("page_view", gorm.Expr("page_view+?", 1))
	if err != nil {
		return article, errmsg.ERROR_ARTICLE_NOT_EXIST
	}
	return article, errmsg.SUCCESS
}

//  查询文章列表

func GetArtList(pageSize, pageNum int) ([]Article, int, int64) {
	var articleList []Article
	var total int64
	err := db.Select("article.id,title, img, created_at, updated_at, `desc`, comment_count, page_view, Category.name").
		Order("created_at desc").Joins("Category").Limit(pageSize).Offset(pageSize * (pageNum - 1)).Find(&articleList).Error
	db.Model(&articleList).Count(&total)
	if err != nil {
		return nil, errmsg.ERROR, 0
	}
	return articleList, errmsg.SUCCESS, total
}

// 搜索文章标题
func SearchArt(pageSize, pageNum int, title string) ([]Article, int, int64) {
	var articleList []Article
	var total int64
	err := db.Select("article.id,title, img, created_at, updated_at, `desc`, comment_count, page_view, Category.name").
		Where("title like ?", "%"+title+"%").Order("created_at desc").Joins("Category").Limit(pageSize).Offset(pageSize * (pageNum - 1)).Find(&articleList).Error
	db.Model(&articleList).Where("title like ?", "%"+title+"%").Count(&total)
	if err != nil {
		return nil, errmsg.ERROR, 0
	}
	return articleList, errmsg.SUCCESS, total
}

// 编辑文章
func EditArt(data *Article, id int) int {
	var article Article
	var maps = make(map[string]interface{})
	maps["title"] = data.Title
	maps["cid"] = data.Cid
	maps["desc"] = data.Desc
	maps["content"] = data.Content
	maps["img"] = data.Img

	err := db.Model(&article).Where("id = ?", id).Updates(maps).Error
	if err != nil {
		return errmsg.ERROR
	}
	return errmsg.SUCCESS
}

// 删除文章
func DeleteArt(id int) int {
	var article Article
	err := db.Where("id = ?", id).Delete(&article).Error
	if err != nil {
		return errmsg.ERROR
	}
	return errmsg.SUCCESS
}
